package com.smartleanx.module.hrm.controller.admin.dept;

import cn.hutool.core.collection.CollUtil;
import com.smartleanx.framework.common.pojo.CommonResult;
import com.smartleanx.module.hrm.dal.dataobject.employee.EmployeeDO;
import com.smartleanx.module.hrm.intergration.PostServiceProvider;
import com.smartleanx.module.hrm.service.employee.EmployeeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import java.util.List;

import static com.smartleanx.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.smartleanx.framework.common.pojo.CommonResult.success;
import static com.smartleanx.module.hrm.enums.ErrorCodeConstants.POST_EXISTS_EMPLOYEE;

@Tag(name = "管理后台 - 劳安心岗位")
@RestController
@RequestMapping("/hrm/post")
@Validated
public class PostController {

    @Resource
    private PostServiceProvider postServiceProvider;

    @Resource
    private EmployeeService employeeService;

    @DeleteMapping("/delete")
    @Operation(summary = "删除岗位")
    @PreAuthorize("@ss.hasPermission('system:post:delete')")
    public CommonResult<Boolean> deletePost(@RequestParam("id") Long id) {
        List<EmployeeDO> empList = employeeService.getEmployeeListByPostId(id);
        if (CollUtil.isNotEmpty(empList)) {
            throw exception(POST_EXISTS_EMPLOYEE);
        }
        postServiceProvider.deletePost(id);
        return success(true);
    }


}
